package com.ttl.bc.stock.request2.db;

import hibernate.util.HibernateUtil;

import java.util.Iterator;
import java.util.List;


import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;

import com.ttl.bc.ic.item.BCItemBean;
import com.ttl.bc.stock.request2.bean.BCStkIssue2;
import com.ttl.bc.stock.request2.bean.BCStkIssueSub2;
import com.ttl.common.db.ADAO;

public class BCStkIssue2DAO extends ADAO {

	private SessionFactory sessionFactory;
	
	public boolean add(Object o) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
			//session.persist(o);
			session.save(o);
			session.getTransaction().commit();
			//sessionFactory.getCurrentSession().close();
			return true;
		}catch(Exception e) {
			System.out.println(e.toString());
			return false;
		}finally {
			session.close();
		}
	}


	@SuppressWarnings("unchecked")
	public void delete(String hql) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            if (resultList.size()>0 ) {
            	BCStkIssue2 h = (BCStkIssue2)resultList.get(0);            	
            	h.setIsCancel(1);
            	h.setiTTL_status("-1");
            	
            	Iterator it = h.getBcStkIssueSub2s().iterator();
            	while(it.hasNext()){
            		BCStkIssueSub2 sub = (BCStkIssueSub2)it.next(); 
            		sub.setIttl_status("-1");
            		sub.setIsCancel(1);
            	}
            	
                session.update(h);
            }            
            			
			session.getTransaction().commit();
						
        } catch (HibernateException he) {
            he.printStackTrace();        
		} finally {
			session.close();
		}
	}

	@SuppressWarnings("unchecked")
	public List find(String hql, int maxResults) {
		List result = null;
		Session session = null;

		try {
			SessionFactory sessionFactory = HibernateUtil.getSessionFactory();			
			session = sessionFactory.openSession();			
			session.beginTransaction();
					
			Query q = session.createQuery(hql);
			if (maxResults>0) {
				q.setMaxResults(maxResults);
			}			
			result = q.list();
			
			session.getTransaction().commit();			

		}catch(Exception e) {			
			System.out.println(e.toString());
		}finally {
			session.close();
		}
		return result;
	}

	
	@SuppressWarnings("unchecked")
	public List findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	
	@SuppressWarnings("unchecked")
	public boolean update(String s, Object o) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            String hql = "FROM BCStkIssue2 h where h.docNo = '"+s+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			
            
//            System.out.println("\n\nresultList = "+q.list().size());
            
            for(int i=0; i<resultList.size(); i++){
            	BCStkIssue2 h = (BCStkIssue2)resultList.get(i);
            	

                session.delete(h);
//                System.out.println("delete docNo="+h.getDocNo());
            }            
            
            BCStkIssue2 stk = (BCStkIssue2)o;
            Iterator it = stk.getBcStkIssueSub2s().iterator();
            while (it.hasNext()) {
				BCStkIssueSub2 sub = (BCStkIssueSub2) it.next();
				
				hql = "FROM BCItemBean WHERE code='"+sub.getItemCode()+"'";
	            org.hibernate.Query q2 = session.createQuery(hql);
	            List resultList2 = q2.list();							
				
	            if( resultList2!=null & resultList2.size()>0) {
	            	BCItemBean bean = (BCItemBean)resultList2.get(0);
					sub.setPrice(bean.getLastBuyPrice());
					sub.setAmount(sub.getQty()*sub.getPrice());
	            }	            
			}
            
			session.save(o);
			session.getTransaction().commit();
			
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
	}


	@SuppressWarnings("unchecked")
	public boolean restore(String hql) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();

            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            if (resultList!=null && resultList.size()>0 ) {
            	BCStkIssue2 h = (BCStkIssue2)resultList.get(0);            	
            	h.setIsCancel(0);
            	h.setiTTL_status("0");
            	
            	Iterator it = h.getBcStkIssueSub2s().iterator();
            	while(it.hasNext()){
            		BCStkIssueSub2 sub = (BCStkIssueSub2)it.next(); 
            		sub.setIttl_status("0");
            		sub.setIsCancel(0);
            	}
            	
                session.update(h);
            }            
            			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
	}


	@SuppressWarnings("unchecked")
	public boolean updateStatus(String poRefNo, String status) {
		Session session = null;

		try {
			sessionFactory = HibernateUtil.getSessionFactory();
			session = sessionFactory.openSession();
			session.beginTransaction();
			
			String hql ="FROM BCStkIssue2 WHERE docNo ='"+poRefNo+"'";
            org.hibernate.Query q = session.createQuery(hql);
            List resultList = q.list();			

            if (resultList!=null && resultList.size()>0) {
	            for(int i=0; i<resultList.size(); i++ ) {
	            	BCStkIssue2 h = (BCStkIssue2)resultList.get(i);
	            	h.setiTTL_status(status);
			    	
	            	Iterator it = h.getBcStkIssueSub2s().iterator();
	            	while (it.hasNext()) {
	            		BCStkIssueSub2 sub = (BCStkIssueSub2) it.next();
						sub.setIttl_status(status);
					}
	            	
	                session.update(h);
	            }            
            }
            			
			session.getTransaction().commit();
			return true;
        } catch (HibernateException he) {
            he.printStackTrace();        
			return false;
		} finally {
			session.close();
		}
	}
}
